package com.eco.fanliapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.SyChannelUserMapper;
import com.eco.fanliapp.entity.SyChannelUser;
import com.eco.fanliapp.service.ISyChannelUserService;
import com.eco.fanliapp.service.RedisTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 渠道用户对照表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@Service
public class SyChannelUserServiceImpl extends ServiceImpl<SyChannelUserMapper, SyChannelUser> implements ISyChannelUserService {

	@Autowired
	private SyChannelUserMapper syChannelUserMapper;

	@Autowired
	private RedisTemplateService redisTemplateService;
	/**
	 * 根据用户获取渠道信息
	 *
	 * @param userId
	 * @param type   1优先从redis获取0从数据库中获取
	 * @return
	 */
	@Override
	public SyChannelUser findSyChannelUserById(Long userId, int type) {
		SyChannelUser syChannelUser= null;
		if (userId == null) {
			return null;
		}
		String redisKeyStr = "app_user_channelUserList_" + userId;
		if (type == 1 && redisTemplateService.hasKey(redisKeyStr)) {
			syChannelUser = redisTemplateService.getTByKey(redisKeyStr, SyChannelUser.class);
		}
		if (syChannelUser == null) {
			QueryWrapper<SyChannelUser> queryWrapper = new QueryWrapper();
			queryWrapper.eq("user_id", userId);
			queryWrapper.eq("enable",1);
			queryWrapper.eq("deleted",0);
			List<SyChannelUser> syChannelUserList = syChannelUserMapper.selectList(queryWrapper);
			if (syChannelUserList != null && syChannelUserList.size() > 0) {
				syChannelUser = syChannelUserList.get(0);
				redisTemplateService.set(redisKeyStr, syChannelUser, 60 * 30);
			}

		}
		return syChannelUser;
	}
}
